#pragma once

#include "../CameraBase.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>

using namespace std;

namespace movesense {

#define REQ_BUF_LEN 4

typedef struct buffer{
	void *start;
	size_t length;
}buffer;


//Video4linux
class CameraV4L : public CameraBase
{
public:
	CameraV4L(CameraInfo info);
	~CameraV4L(void);

private:
	string dev_name;
	int fd;
	struct buffer buffers[REQ_BUF_LEN];
	
private:
	int xioctl(int fd, int request, void * arg);
	int set_format(int width, int height);
	int request_and_mmap();
	int unmmap();

private:
	int SetSaturation(long value);
	int GetSaturation(long *value);
	int SetWhiteBalance(long value);
	int GetWhiteBalance(long *value);


public:
	int Open(int width, int height);
	void Close();

	int StartStream();
	int StopStream();

	int GetImageData(unsigned char *data, int &len, int ms);
};

}